<?php 

class AdminModel extends Model{
	public $table = 'admin';
	/**
	 * 自动验证
	 */
	public $validate = array(
		array('username','nonull','帐号不能为空',2,3),
		array('password','nonull','密码不能为空',2,3),
		array('code','nonull','验证码不能为空',2,3),
	);
	public function login(){
	    //create 触发自动验证
		if(!$this->create()) return false;
		//验证码是否正确
		if(Q('post.code','','strtoupper') != session('code')){
			//此处压入错误，就是为了在控制器中调用错误信息
			$this->error = '验证码不正确';
			return false;
		}
		//验证用户名和密码是否正确
		$username = Q('post.username');
		$password = Q('post.password','','md5');
		//通过用户名获得数据
		$userInfo = $this->where("accounts='{$username}'")->find();
		//用户名不存在或者密码不正确
		if(!$userInfo || $password != $userInfo['password']){
			$this->error = '用户名或者密码不正确';
			return false;
		}
		$userInfo['addrip'] = $_SERVER['REMOTE_ADDR'];
		$userInfo['time'] = $_SERVER['REQUEST_TIME'];
		$this->where("aid=$userInfo[aid]")->update($userInfo);
		//返回用户信息
		return $userInfo;
	}
}
